package com.domain.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.domain.pojo.FriendOfFriend;
import com.domain.service.GraphService;
import com.domain.utils.NotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

@Service
public class GraphServiceImpl implements GraphService {
/*
    @Autowired
    private JdbcTemplate template;

    @Bean
    public javax.sql.DataSource dataSource() {
        String NEO4J_URL = System.getenv("NEO4J_URL");
        if (NEO4J_URL == null)
            NEO4J_URL = System.getProperty("NEO4J_URL", "jdbc:neo4j:http://192.168.254.9:7474");//bolt://localhost:7687
//	     if (NEO4J_URL==null) NEO4J_URL=System.getProperty("NEO4J_URL","bolt://192.168.254.9:7687");//bolt://localhost:7687
        DriverManagerDataSource ds = new DriverManagerDataSource(NEO4J_URL);
        ds.setUsername("neo4j");
        ds.setPassword("123456");
        return ds;
    }

    private final RowMapper<FriendOfFriend> FOF_ROW_MAPPER = new RowMapper<FriendOfFriend>() {
        public FriendOfFriend mapRow(ResultSet rs, int rowNum) throws SQLException {
            return new FriendOfFriend(rs.getString("uid"), rs.getInt("count"));
        }
    };


    private final RowMapper<JSONObject> JSON_ROW_MAPPER = new RowMapper<JSONObject>() {
        public JSONObject mapRow(ResultSet rs, int rowNum) throws SQLException {
            JSONObject json = new JSONObject();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int index = 1; index <= columnCount; index++) {
                String column = JdbcUtils.lookupColumnName(rsmd, index);
                Object value = rs.getObject(column);
                json.put(column, "null".equalsIgnoreCase(value.toString()) ? "" : value);
            }
            return json;
        }
    };


    *//**
     * 添加新用户
     *
     * @param usersId
     * @param personIUCode
     * @return
     *//*
    @Override
    public int addUser(String usersId, String personIUCode) {
        String sql = "MERGE (n:Person {uid:'%d', personIUCode:'%s'})";

        sql = String.format(sql, usersId, personIUCode);

        this.template.execute(sql);

        return 1;
    }

    *//**
     * 添加新好友关系
     *
     * @param usersId
     * @param friendsId
     * @return
     *//*
    @Override
    public int addFriend(String usersId, String friendsId) {
        String sql = "Match(a:Person {uid:'%d'}) WITH(a) Match(b:Person {uid:'%d'}) WITH b,a MERGE (a)-[:F]->(b) MERGE (b)-[:F]->(a)  return a,b ";

        sql = String.format(sql, usersId, friendsId);

        this.template.execute(sql);

        return 1;
    }

    *//**
     * 获取好友的好友
     *
     * @param usersId
     * @param personIUCode
     * @return
     *//*
    @Override
    public List<FriendOfFriend> getFriendOfFriend(String usersId, String personIUCode) {
        String sql = "Match (user:Person {uid:{1}}) match (user)-[:F]->(friend:Person {personIUCode:{2}})-[:F]->(fof:Person {personIUCode:{2}}) with user,fof OPTIONAL Match (user)-[r]-(fof) with fof,r where r is null  RETURN fof.uid as uid, COUNT(*) as count ORDER BY COUNT(*) DESC, fof.uid limit 20";
        List<FriendOfFriend> res = template.query(sql, new Object[]{usersId + "", personIUCode}, FOF_ROW_MAPPER);
        return res;
    }

    *//**
     * @param usersId
     * @param mobiles
     *//*
    @Override
    public void addMobileAddress(String usersId, Set<String> mobiles) {
        StringBuffer sql = new StringBuffer("MERGE (p:Person {uid:" + usersId + "}) ");

        String oneMobile = "MERGE (m%s:M {num:'%s'}) ";
        String oneHas = "MERGE (p)-[:H]->(m%s) ";

        for (String mobile : mobiles) {

            if (!NotNullUtils.isMobile(mobile)) {
                continue;
            }

            sql.append(String.format(oneMobile, mobile, mobile))
                    .append(String.format(oneHas, mobile));
        }
        System.out.println(sql.toString());
        this.template.execute(sql.toString());
    }

    *//**
     * 我的通讯录联系人在人脉通中注册，但和我还不是好友（我有他的手机号码）
     *
     * @param usersId
     * @param page
     * @param pageSize
     * @return
     *//*
    @Override
    public List<JSONObject> getMyHasMobile(String usersId, int page, int pageSize) {

        String sql = "MATCH (a:Person {uid:{1}})-[:H]->(m)-[:Belong]->(b:Person) WHERE NOT (a)-[:F]-(b) RETURN b.uid AS uid,1 AS type SKIP {2} LIMIT {3}";

        int skip = (page - 1) * pageSize < 0 ? 0 : (page - 1) * pageSize;

        List<JSONObject> res = template.query(sql, new Object[]{usersId + "", skip, pageSize}, JSON_ROW_MAPPER);

        return res;
    }

    *//**
     * 拥有我的号码，但不是好友（他有我的手机号码）
     *
     * @param usersId
     * @param page
     * @param pageSize
     * @return
     *//*
    @Override
    public List<JSONObject> getOtherHasMyMobile(String usersId, int page, int pageSize) {
        String sql = "MATCH (a:Person {uid:{1}})<-[:Belong]-(m)<-[:H]-(b:Person) WHERE NOT (a)-[:F]-(b) RETURN b.uid AS uid,2 AS type SKIP {2} LIMIT {3}";

        int skip = (page - 1) * pageSize < 0 ? 0 : (page - 1) * pageSize;

        List<JSONObject> res = template.query(sql, new Object[]{usersId + "", skip, pageSize}, JSON_ROW_MAPPER);

        return res;
    }


    @Override
    public List<JSONObject> getWeHasSameMobile(String usersId, int page, int pageSize) {
        return null;
    }

    @Override
    public List<JSONObject> getTheSameFriend(String usersId, int page, int pageSize) {
        return null;
    }

    @Override
    public List<JSONObject> getSameFriend(String usersId1, String usersId2, int page, int pageSize) {
        return null;
    }*/
}
